//
//  Utils.m
//  iSudoku
//
//  Created by Tri Vo on 5/22/13.
//  Copyright (c) 2013 Tri Vo. All rights reserved.
//

#import "GameUtils.h"

static GameUtils *instance = nil;

@implementation GameUtils

+ (id) getInstance {
    if (!instance) {
        instance = [[GameUtils alloc] init];
    }
    return instance;
}

#pragma mark - Settings

- (BOOL)isMusicOn {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"SETTING_MUSIC"] boolValue];
}

- (void)setMusicOn:(BOOL)bVal {
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:bVal] forKey:@"SETTING_MUSIC"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isSoundOn {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"SETTING_SOUND"] boolValue];
}

- (void)setSoundOn:(BOOL)bVal {
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:bVal] forKey:@"SETTING_SOUND"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *) getCurrentLanguage {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"SETTING_LANGUAGE"];
}

- (void) loadLanguage {
    NSString *languagePath = [[NSBundle mainBundle] pathForResource:[self getCurrentLanguage] ofType:@"plist"];
    appLanguages = nil;
    appLanguages = [[NSDictionary dictionaryWithContentsOfFile:languagePath] retain];
}

- (NSString *) getValueForKey:(NSString *)key {
    return [appLanguages objectForKey:key];
}

- (void) setLanguage:(BOOL)en {
    if (en) {
        [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"SETTING_LANGUAGE"];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"vi" forKey:@"SETTING_LANGUAGE"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *) getSystemFontName {
    return @"Arial";
}

#pragma mark - Game

- (int)getCurrentLevel {
    return mCurrentLevel;
}

- (void)setCurrentLevel:(int)level {
    mCurrentLevel = level;
}

-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
    sprite.scaleX = width / sprite.contentSize.width;
    sprite.scaleY = height / sprite.contentSize.height;
}

-(NSString *) getDateWithLong:(CGFloat)lTime {
    // Format time
    NSTimeInterval seconds = lTime/1000;
    NSDate* dateValue = [NSDate dateWithTimeIntervalSince1970:seconds];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString* dateString = [dateFormatter stringFromDate:dateValue];
    [dateFormatter release];
    return dateString;
}

-(int) getGameMode {
    return gameMode;
}
-(void) setGameMode:(int)mode {
    gameMode = mode;
}

-(void) doBack {
    [[CCDirector sharedDirector] popSceneWithTransition:[CCTransitionSlideInL class] withTime:0.5f];
}

@end
